﻿namespace ScrumLite.Data.Tests
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Transactions;
    using Microsoft.VisualStudio.TestTools.UnitTesting;

    /// <summary>
    /// Summary description for MeasureStoreFixture
    /// </summary>
    [TestClass]
    public class MeasureStoreFixture
    {
        IMeasureStore store;
        Guid projectId;

        [TestInitialize]
        public void Initialize()
        {
            this.store = new MeasureStore();
            projectId = new Guid("1C4F9968-8D5E-4d8a-9BD8-9A8DF1434B5A");
        }        

        [TestMethod()]
        public void ShouldGetMeasuresByProcessName()
        {
            using (TransactionScope scope = new TransactionScope())
            {
                CreateFakeMeasureStore();

                Measure[] actual = this.store.GetMeasures("FakeProcess");

                Assert.AreEqual(2, actual.Length);
            }
        }

        [TestMethod()]
        public void ShouldGetProjectMeasuresByProcessName()
        {
            using (TransactionScope scope = new TransactionScope())
            {
                CreateFakeMeasureStore();

                ProjectMeasure[] actual = this.store.GetProjectMeasures(projectId, "FakeProcess");

                Assert.AreEqual(2, actual.Length);
            }
        }

        private static void CreateFakeMeasureStore()
        {
            Guid projectId = new Guid("1C4F9968-8D5E-4d8a-9BD8-9A8DF1434B5A");
            Guid processId = Guid.NewGuid();
            Guid measureId1 = Guid.NewGuid();
            Guid measureId2 = Guid.NewGuid();
            Guid pmId1 = Guid.NewGuid();
            Guid pmId2 = Guid.NewGuid();

            using (ScrumLiteDataContext context = new ScrumLiteDataContext())
            {
                Project project = new Project() { endsOn = DateTime.Today, id = projectId, name = "FakeProject", security_group = "", startsOn = DateTime.Today, uri_repository = "", user_ownedBy = "" };
                context.Projects.InsertOnSubmit(project);

                Process process = new Process() { id = processId, name = "FakeProcess" };
                context.Processes.InsertOnSubmit(process);

                Measure measure1 = new Measure() { id = measureId1, fk_process = processId, name = "measure1", lowTresholdValue = 5, highTresholdValue = 10, unit = "u1", ascendentMeasure = true };
                Measure measure2 = new Measure() { id = measureId2, fk_process = processId, name = "measure2", lowTresholdValue = 5, highTresholdValue = 10, unit = "u2", ascendentMeasure = true };
                context.Measures.InsertOnSubmit(measure1);
                context.Measures.InsertOnSubmit(measure2);

                ProjectMeasure pm1 = new ProjectMeasure() { id = pmId1, fk_measure = measureId1, fk_project = projectId, value = 4, lastMeasure = DateTime.Now };
                ProjectMeasure pm2 = new ProjectMeasure() { id = pmId2, fk_measure = measureId2, fk_project = projectId, value = 15, lastMeasure = DateTime.Now };
                context.ProjectMeasures.InsertOnSubmit(pm1);
                context.ProjectMeasures.InsertOnSubmit(pm2);

                context.SubmitChanges();
            }
        }
    }
}
